<template>
  <div id="addSpacePage">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '修改' : '创建' }}{{SPACE_TYPE_MAP[spaceType]}}
    </h2>
    <!--    输入空间信息功能-->
    <a-form layout="vertical" :model="formData" @finish="handleSubmit">
      <a-form-item label="空间名称" name="spaceName">
        <a-input v-model:value="formData.spaceName" placeholder="请输入空间名" allow-clear />
      </a-form-item>
      <a-form-item label="空间级别" name="spaceLevel">
        <a-select
          v-model:value="formData.spaceLevel"
          :options="SPACE_LEVEL_OPTIONS"
          placeholder="请选择空间级别"
          style="min-width: 180px"
          allowClear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%" :loading="loading">
          {{ route.query?.id ? '更新空间' : '创建空间' }}
        </a-button>
      </a-form-item>
    </a-form>
    <!--    展示空间级别介绍-->
    <a-card title="空间级别介绍">
      <a-typography-paragraph>
        * 目前仅支持开通普通版，如需升级空间，请联系
        <a href="https://space.bilibili.com/432460619" target="_blank">王子琦</a>
      </a-typography-paragraph>
      <a-typography-paragraph v-for="spaceLevel in spacelevelLst">
        {{ spaceLevel.text }}: 大小 {{ formatSize(spaceLevel.maxSize) }}, 数量
        {{ spaceLevel.maxCount }}
      </a-typography-paragraph>
    </a-card>
  </div>
</template>
<script setup lang="ts">
import {computed, onMounted, reactive, ref} from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
  addSpaceUsingPost,
  getSpaceVoByIdUsingGet,
  listSpaceLevelUsingGet,
  updateSpaceUsingPost,
} from '@/api/spaceController'
import { message } from 'ant-design-vue'
import {SPACE_LEVEL_ENUM, SPACE_LEVEL_OPTIONS, SPACE_TYPE_ENUM, SPACE_TYPE_MAP} from '@/constants/space'
import { formatSize } from '../utils'

const router = useRouter()
const route = useRoute()

const formData = reactive<API.SpaceEditRequest | API.SpaceAddRequest>({
  spaceName: '',
  spaceLevel: SPACE_LEVEL_ENUM.COMMON,
}) // 表单对象

const loading = ref(false)

/**
 * 提交表单函数
 * @param values
 */
const handleSubmit = async (values: any) => {
  const spaceId = oldSpace.value?.id
  // 加载按钮变化
  loading.value = true
  let res
  // 如果spaceid本就存在则调用更新接口，否则新建空间
  if (spaceId) {
    res = await updateSpaceUsingPost({
      id: spaceId,
      ...formData,
    })
  } else {
    res = await addSpaceUsingPost({
      ...formData,
      spaceType: spaceType.value
    })
  }
  // 操作成功跳转
  if (res.data.code === 0 && res.data.data) {
    message.success('空间创建成功')
    let path = `/space/${spaceId ?? res.data.data}`
    //跳转到空间详情页
    router.push({
      path,
    })
  } else {
    message.error('空间创建失败，' + res.data.message)
  }
  loading.value = false //创建成功取消按钮加载状态
}

const spacelevelLst = ref<API.SpaceLevel[]>([])

/**
 * 获取空间级别
 */
const fetchSpaceLevelList = async () => {
  const res = await listSpaceLevelUsingGet()
  if (res.data.code === 0 && res.data.data) {
    spacelevelLst.value = res.data.data
  } else {
    message.error('加载空间级别失败 ' + res.data.message)
  }
}

/**
 * 空间类别
 */
const spaceType = computed(() => {
  if(route.query?.type) {
    return Number(route.query.type);
  }
  return SPACE_TYPE_ENUM.PRIVATE;
})

/**
 * 空间信息修改
 */
const oldSpace = ref<API.SpaceVO>()
// 获取旧数据
const getOldSpace = async () => {
  const id = route.query?.id
  if (id) {
    const res = await getSpaceVoByIdUsingGet({
      id: id,
    })
    if (res.data.code === 0 && res.data.data) {
      const data = res.data.data
      oldSpace.value = data
      formData.spaceName = data.spaceName
      formData.spaceLevel = data.spaceLevel
    }
  }
}

onMounted(() => {
  fetchSpaceLevelList()
  getOldSpace()
})
</script>
<style scoped>
#addSpacePage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
